package com.bosssoft.trainee.nontax.common.result;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/*
 * @Description: 返回统一结果集
 * @Author: Luo WenLong
 * @Date: 2022/6/26 14:14
 */
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> {

    private String message;
    private int code;
    private T data;

    private Result(T data) {
        this.code = 20000;
        this.message = "success";
        this.data = data;
    }

    /**
     * @Description: constructor
     * @Author: Luo WenLong
     * @Date: 2022/6/26 14:16
     */
    private Result(CodeMsg cm) {
        if(cm == null){
            return;
        }
        this.code = cm.getRetCode();
        this.message = cm.getMessage();
    }

    /**
     * @Description: 成功时候的调用
     * @return: com.bosssoft.trainee.nontax.common.result.Result<T>
     * @Author: Luo WenLong
     * @Date: 2022/6/26 14:14
     */
    public static <T> Result<T> success(T data){
        return new Result<>(data);
    }

    /**
     * @Description: 成功，不需要传入参数
     * @return: com.bosssoft.trainee.nontax.common.result.Result<T>
     * @Author: Luo WenLong
     * @Date: 2022/6/26 14:14
     */
    public static <T> Result<T> success(){
        return (Result<T>) success("");
    }
    /**
     * @Description: 失败时候的调用
     * @return: com.bosssoft.trainee.nontax.common.result.Result<T>
     * @Author: Luo WenLong
     * @Date: 2022/6/26 14:14
     */
    public static <T> Result<T> error(CodeMsg cm){
        return new Result<>(cm);
    }

    /**
     * @Description: 失败时候的调用,扩展消息参数
     * @return: com.bosssoft.trainee.nontax.common.result.Result<T>
     * @Author: Luo WenLong
     * @Date: 2022/6/26 14:15
     */
    public static <T> Result<T> error(CodeMsg cm,String msg){
        cm.setMessage(cm.getMessage()+"--"+msg);
        return new Result<>(cm);
    }
}
